package com.example.demo_text;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.*;

@WebServlet("/AdmitUpdateTeacherServlet")
public class AdmitUpdateTeacherServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");

        String idStr = request.getParameter("id");
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        String ageStr = request.getParameter("age");

        int id, age;

        try {
            id = Integer.parseInt(idStr);
            age = Integer.parseInt(ageStr);
        } catch (NumberFormatException e) {
            response.sendRedirect("admitmanageTeacher.jsp");
            return;
        }

        boolean success = UserDAO.updateTeacherProfile(id, username, email, age);

        if (success) {
            response.sendRedirect("manageTeacher.jsp?id=" + id);
        } else {
            request.setAttribute("error", "更新失败，请稍后再试");
            request.getRequestDispatcher("manageTeacher.jsp").forward(request, response);
        }
    }
}
